#include "modifygoods.h"
#include "ui_modifygoods.h"

Modifygoods::Modifygoods(QWidget *parent,int sid) :
    QDialog(parent),
    ui(new Ui::Modifygoods)
{
    m_sid = sid;
    myDatabase.getConnection();


    ui->setupUi(this);
}

Modifygoods::~Modifygoods()
{
    delete ui;
}

void Modifygoods::on_Modify_clicked()
{
    QString name = ui->name->text();
    int num = ui->num->text().toInt();
    float price = ui->price->text().toFloat();
    QString info = ui->info->text();

    QSqlQuery sql0;
    sql0.prepare("SELECT* FROM tb_goodsinstore WHERE sid = :sid AND name = :name");
    sql0.bindValue(":sid",m_sid);
    sql0.bindValue(":name",name);
    sql0.exec();
    if(!sql0.first()){
        QMessageBox::information(this,"提示","没有此货物！",QMessageBox::Ok);
    }
    else{
        QSqlQuery sql;
        sql.prepare("UPDATE tb_goodsinstore SET num = :num,price = :price,info = :info WHERE sid = :sid AND name = :name");
        sql.bindValue(":sid",m_sid);
        sql.bindValue(":num",num);
        sql.bindValue(":price",price);
        sql.bindValue(":info",info);
        sql.bindValue(":name",name);
        sql.exec();
        QMessageBox::information(this,"提示","更新成功！",QMessageBox::Ok);
    }
    this->~Modifygoods();
}


void Modifygoods::on_display_clicked()
{
    QString name = ui->name->text();

    QSqlQuery sql;
    sql.prepare("SELECT* FROM tb_goodsinstore WHERE sid = :sid AND name = :name");
    sql.bindValue(":sid",m_sid);
    sql.bindValue(":name",name);
    sql.exec();
    if(!sql.first()){
        ui->tips->setText("此货物不存在！");
    }
    else{
        ui->num->setText(sql.value("num").toString());
        ui->price->setText(sql.value("price").toString());
        ui->info->setText(sql.value("info").toString());
        ui->tips->setText("找到该货物！");
    }
}

